<?php

/*
	存放一些公共调用、和不需要登录验证的接口
*/

namespace api\modules\v1\controllers;

use Yii;
use api\modules\models\member\Member;
use common\utils\ChuangnanSms;
use yii\db\Query;
use common\models\DB;
use common\models\SmsModel;
use common\utils\FileUpload;
use crazyfd\qiniu\Qiniu;
use common\service\vesrpc\ves;

class UpdateController extends CoreController
{
	public function actionTest()
	{

		$data = ves::balance_query(1,'BTC');
		return $this->out('请求成功',$data);
	}

	## 版本检测 更新
	public function actionVersionCheck()
	{
		$name = $this->request('name');  //app名称
		$versionCode =  $this->request('versionCode',0); //app大版本号  用于下载更新
		$versionName = $this->request('versionName',0); //app版本号 如 1.1.0; 用于热更新
		if (!$name || !$versionName || !$versionName) return $this->error('参数错误');

		$file = Yii::$app->params['apiPath'].'/app/conf.php';
		$conf = require_once($file);

		//当前最新发布的大版本
		$releaseCode = $conf['versionCode'];
		//当前最新发布的热更新版本
		$releaseVersion = $conf['versionName'];
		//用于更新app名称
		$appName = $conf['appName'];
		$appid = $conf['appid'];

		if ($versionCode < $releaseCode) {
		    $data = array(
		        'update' 		=> true,
		        'wgtUrl' 		=> "",
		        'downloadUrl' 	=> \Yii::$app->request->hostInfo.'/app/download.html',
		    );
		    return $this->out('请求成功',$data);
		}

		//检测是否有热更新
		if ($this->isHotUp($versionName,$releaseVersion))
		{
		    $data = array(
		        'update' 		=> true,
		        'wgtUrl' 		=> \Yii::$app->request->hostInfo.'/app/'.$appid.'.wgt',
		        'downloadUrl' 	=> "",
		    );
		    return $this->out('请求成功',$data);
		} else {
		    $data = array(
		        'update' 		=> false,
		        'wgtUrl' 		=> "",
		        'downloadUrl' 	=> ""
		    );
		    return $this->out('请求成功',$data);
		}
	}


	//是否热更新
	private function isHotUp($versionName,$releaseVersion)
	{
		$versionName = explode('.',$versionName);
		$releaseVersion = explode('.',$releaseVersion);
		if($releaseVersion[0]>$versionName[0]) return true;

		if($releaseVersion[0]>=$versionName[0] && $releaseVersion[1]>$versionName[1]) return true;
		if($releaseVersion[0]>=$versionName[0] && $releaseVersion[1]>=$versionName[1] &&  $releaseVersion[2]>$versionName[2]) return true;
		return false;
	}

}